from django.shortcuts import render
from rest_framework.generics import CreateAPIView,ListAPIView
from rest_framework.permissions import IsAuthenticated
from orders.models import OrderModel
from orders.serial import OrderSerializer,OrderListModelSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
# Create your views here.

# 生成订单
class OrderCreateAPIView(CreateAPIView):
    permission_classes = [IsAuthenticated]
    queryset = OrderModel.objects.all()
    serializer_class = OrderSerializer

# 获取订单状态选项
class OrderPayStatusAPIView(APIView):
    def get(self,request):
        return Response(OrderModel.ORDER_STATUS_CHOICES)

# 分页面
class OrderPageNumberPagination(PageNumberPagination):
    page_query_param = 'page'
    page_size = 10
    page_size_query_param = 'size'
    max_page_size = 10

# 获取订单列表
class OrderListAPIView(ListAPIView):
    permission_classes = [IsAuthenticated]
    pagination_class = OrderPageNumberPagination
    serializer_class = OrderListModelSerializer

    def get_queryset(self):
        user = self.request.user
        query_set = OrderModel.objects.filter(user=user,is_deleted=False,is_show=True)
        order_status = int(self.request.query_params.get('status',-1))
        print('获取前端传递的状态：',order_status)
        # 动态获取配置的订单状态列表
        status_list = [item[0] for item in OrderModel.ORDER_STATUS_CHOICES]
        # 匹配前传递的订单状态，是否在状态列表中
        print('-------------------------------')
        if order_status in status_list:
            query_set = query_set.filter(order_status=order_status)
            print('查询条件：',query_set)
        return query_set.order_by('-id')